/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div class="entrance">
    <!-- 顶部按钮区域 -->
    <div class="top-buttons" :style="{
      paddingTop: `${topMargin + 8}px`
    }">
      <img src="../assets/home_pause.png" alt="" class="top-image" :style="{
        paddingLeft: `${margins.leftTopMargin + 16}px`
      }">
      <div class="right-buttons-group" :style="{
        paddingRight: `${margins.rightTopMargin + 16}px`,
      }">
        <img src="../assets/mute.png" alt="" class="top-image">
        <img src="../assets/rotate.png" alt="" class="top-image">
        <img src="../assets/setting.png" alt="" class="top-image">
      </div>
    </div>

    <!-- 中间内容区域 -->
    <div class="middle-content">
      <img src="../assets/home_icon.png" alt="" class="main_image">
      <div class="button-container">
        <img src="../assets/arrowhead.png" alt="" class="arrowhead">
        <router-link to="/game" style="height:20px">
          <img src="../assets/start.png" alt="" class="options">
        </router-link>
      </div>
      <div class="button-container">
        <img src="../assets/arrowhead.png" alt="" class="arrowhead" style="visibility:hidden">
        <router-link to="/game" style="height:20px">
          <img src="../assets/login.png" alt="" class="options">
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { AreaUtils } from '../utils/AreaUtils';
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue';
import { AvoidAreaType } from '../common/windowCommon';

const margins = reactive({
  leftTopMargin: 0,
  rightTopMargin: 0,
  leftBottomMargin: 0,
  rightBottomMargin: 0,
  leftCenterMargin: 0,
  rightCenterMargin: 0
});
const topMargin = ref(AreaUtils.getInstance().getTopMargin());

const reCulcAvoidArea = () => {
  AreaUtils.getAvoidAreaLocation().then((res) => {
    AreaUtils.getInstance().setAvoidValue(res.areaLocation, res.marginValue);
    Object.assign(margins, AreaUtils.getAllMargin());
  });
};

const handleStatusBarChange = (event: any) => {
  const data = event.detail;
  const areaType = data.type;
  if (areaType === AvoidAreaType.TYPE_SYSTEM) {
    topMargin.value = AreaUtils.px2vp(data.area.topRect.height);
    AreaUtils.getInstance().setTopMargin(topMargin.value);
  }
  if (areaType === AvoidAreaType.TYPE_CUTOUT) {
    reCulcAvoidArea();
  }
  if (areaType === AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
    let bottomMargin = AreaUtils.px2vp(data.area.bottomRect.height);
    AreaUtils.getInstance().setBottomMargin(bottomMargin);
  }
};

onMounted(() => {
  reCulcAvoidArea();
  window.addEventListener("resize", reCulcAvoidArea);
  window.addEventListener("statusBarChange", handleStatusBarChange);
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", reCulcAvoidArea);
  window.removeEventListener("statusBarChange", handleStatusBarChange);
});
</script>

<style scoped>
.entrance {
  height: 100vh;
  width: 100%;
  background-image: url('../assets/background.png');
  background-size: auto 100%;
  background-position: center;
  background-attachment: fixed;
  justify-content: start;
  align-items: center;
  display: flex;
  flex-direction: column;
}

/* 顶部按钮区域样式 */
.top-buttons {
  display: flex;
  height: 60px;
  width: 100%;
  justify-content: space-between;
  gap: 8px
}

.top-image {
  height: 40px;
  width: 40px;
}

.right-buttons-group {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

/* 中间内容区域样式 */
.middle-content {
  position: absolute;
  flex: 1;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}

.main_image {
  width: auto;
  height: 130px;
  margin-bottom: 30px;
}

.button-container {
  height: 27px;
  display: flex;
  gap: 20px;
  flex-direction: row;
  align-items: center;
}

.arrowhead {
  height: 14px;
}

.options {
  height: 20px;
}
</style>
